<?php
/**
 * Created by PhpStorm.
 * User: yujiayong
 * Date: 2017/3/16
 * Time: 16:12
 */

namespace Org\Util;


class DownloadWord
{
    public function getArchiveWord($type, $archiveArr, $otherTableArr,$docName)
    {
        vendor("PHPWord");
        require_cache(VENDOR_PATH . 'PHPWord/PHPWord.php');
        $PHPWord = new \PHPWord();
        if ($type == 1) {
            $this->getBaseWord($PHPWord, $archiveArr['basicInfo'], $archiveArr['carInfo'], $archiveArr['ownerInfo']);
            $this->getRepairWord($PHPWord, "车辆维护和修理登记表", array("维护日期", "累计行驶里程(km)", "维修类别", "修理内容", "维修单位", "合格证编号", "登记人员"), $archiveArr['repair'], "注：车辆维修类别栏应填一级维护、二级维护、小修、大修或总成修理");
            $this->getRepairWord($PHPWord, "车辆检测和评定登记表", array("检测评定类别", "检测评定单位", "检测评定日期", "检测有效期", "报告编号", "备注", "登记人员"), $archiveArr['evaluate'], "注: 主要登记发动机、离合器、车厢、驾驶室、转向器、变速器、前桥、后桥、车架及轮胎部件的更换情况。");
            $this->getRepairWord($PHPWord, "车辆主要部件更换登记表", array("更换日期", "部件名称", "型号规格", "生产厂名称", "部件编号", "维修单位", "登记人员"), $archiveArr['parts'], "注: 主要登记发动机、离合器、车厢、驾驶室、转向器、变速器、前桥、后桥、车架及轮胎部件的更换情况。");
            $this->getRepairWord($PHPWord, "车辆变更登记表", array("变更日期", "变更原因", "变更事项", "登记人员"), $archiveArr['change'], "注：变更事项指车辆停驶、封存、启封使用、报废等变更情况，不包括车主名称、道路运输证号和车牌号的变更。");
            $this->getRepairWord($PHPWord, "车辆行驶里程登记表", array("登记日期", "当月行驶里程（km）", "累计行驶里程（km）", "登记人员"), $archiveArr['mileage'], "注：行驶里程按月进行登记。");
            $this->getRepairWord($PHPWord, "车辆机损事故登记表", array("事故时间", "事故地点", "事故性质", "事故责任", "登记人员"), $archiveArr['accident'], "注1：事故性质指《生产安全事故报告和调查处理条例》（国务院令第493号）规定的特别重大事故、重大事故、较大事故或一般事故。
注2：事故责任指全部责任、主要责任、同等责任、次要责任或无责任。");
            $this->getRepairWord($PHPWord, "保险录入表格", array("驾驶员", "从业资格证证号", "联系电话", "终止日期", "保险公司", "登记人员"), $archiveArr['insurance']);
        } elseif ($type == 2) {
            $this->getOtherWord($PHPWord, $otherTableArr);
        }
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition:attachment;filename=".$docName.".docx");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
        $objWriter->save('php://output');
    }

    private function getBaseWord($PHPWord, $basicInfo, $carInfo, $ownerInfo)
    {
        $section = $PHPWord->createSection();
        $styleTable = array('borderSize' => 1, 'borderColor' => '000000', 'cellMargin' => 80);
        $styleFirstRow = array('borderBottomSize' => 1, 'borderBottomColor' => '000000');
        $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
        $table = $section->addTable('myOwnTableStyle');
        $this->addBaseRow($table, $basicInfo, $carInfo, $ownerInfo);
    }

    /**
     * @param $PHPWord
     * @param $docTitle 表名
     * @param $tableHeader 表头信息
     * @param $tableContent 表的内容信息
     * @param string $note 注释内容信息
     */
    private function getRepairWord($PHPWord, $docTitle, $tableHeader, $tableContent, $note = "")
    {
        if (!empty($tableContent)) {
            $section = $PHPWord->createSection();
            $styleTable = array('borderSize' => 1, 'borderColor' => '000000', 'cellMargin' => 80);
            $styleFirstRow = array('borderBottomSize' => 1, 'borderBottomColor' => '000000');
            $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
            $styleCell = array('valign' => 'center');
            $countTable = count($tableHeader);
            $styleCell1 = array('valign' => 'center', 'gridSpan' => $countTable + 1);
            $fontStyle = array('bold' => true, 'align' => 'center');
            $styleParagraph = array('align' => 'center');
            $table = $section->addTable('myOwnTableStyle');
            $table->addRow();
            $table->addCell(1800, $styleCell1)->addText($docTitle, $fontStyle, $styleParagraph);
            $table->addRow();
            $table->addCell(500, $styleCell)->addText("序号", null, $styleParagraph);
            for ($k = 0; $k < $countTable; $k++) {
                $table->addCell(1800, $styleCell)->addText($tableHeader[$k], null, $styleParagraph);
            }
            $countRepair = count($tableContent);
            for ($i = 0; $i < $countRepair; $i++) {
                $table->addRow();
                $table->addCell(500, $styleCell)->addText($i + 1, null, $styleParagraph);
                for ($j = 0; $j < $countTable; $j++) {
                    $table->addCell(1800, $styleCell)->addText($tableContent[$i][$j], null, $styleParagraph);
                }
            }
            if (!empty($note)) {
                $table->addRow();
                $table->addCell(1800, $styleCell1)->addText($note);
            }
        }
    }

    private function getOtherWord($PHPWord, $otherTableArr)
    {
        if (!empty($otherTableArr['titles']) && !empty($otherTableArr['contents'])) {
            $section = $PHPWord->createSection();
            $styleTable = array('borderSize' => 1, 'borderColor' => '000000', 'cellMargin' => 20);
            $styleFirstRow = array('borderBottomSize' => 1, 'borderBottomColor' => '000000');
            $fontStyle = array('bold' => true, 'align' => 'center');
            $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
            $table = $section->addTable('myOwnTableStyle');
            $table->addRow();
            $cotTitle = count($otherTableArr['titles']);
            $cotContent = count($otherTableArr['contents']);
            if (!in_array("操作",$otherTableArr['titles'])) {
                $cols = $cotTitle;
            } else {
                $cols = $cotTitle - 1;
            }
            for ($a = 0; $a < $cols; $a++) {
                $table->addCell(2000)->addText($otherTableArr['titles'][$a], $fontStyle);
            }
            if (!empty($otherTableArr['contents'])) {
                for ($b = 0; $b < $cotContent; $b++) {
                    $table->addRow();
                    for ($c = 0; $c < $cols; $c++) {
                        $table->addCell(2000)->addText($otherTableArr['contents'][$b][$c]);
                    }
                }
            }
        }
    }

    private function addBaseRow($table, $basicInfo, $carInfo, $ownerInfo)
    {
        $styleParagraph = array('align' => 'center');
        $styleCell = array('valign' => 'center');
        $styleCell1 = array('valign' => 'center', 'gridSpan' => 7);
        $styleCell2 = array('valign' => 'center', 'gridSpan' => 5);
        $styleCell3 = array('valign' => 'center', 'gridSpan' => 2, 'vMerge' => 'restart');
        $styleCell4 = array('valign' => 'center', 'gridSpan' => 2);
        $styleCell5 = array('valign' => 'center', 'gridSpan' => 2, 'vMerge' => 'fusion');
        $styleCell6 = array('valign' => 'center', 'vMerge' => 'restart');
        $styleCell7 = array('valign' => 'center', 'vMerge' => 'fusion');
        $styleCell8 = array('valign' => 'center', 'gridSpan' => 6);
        $styleCell9 = array('valign' => 'center', 'gridSpan' => 7);
        $fontStyle = array('bold' => true, 'align' => 'center');
        $table->addRow();
        $table->addCell(1800, $styleCell1)->addText("车辆基本信息表", $fontStyle, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell2)->addText("车辆号牌信息", null, $styleParagraph);
        $table->addCell(1800, $styleCell3)->addText("粘贴初次办理或变更《道路运输证》时，车辆正面偏右侧45º的3寸彩色照片", array(), $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell)->addText("", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("车牌号码", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("颜色", null, $styleParagraph);
        $table->addCell(1800, $styleCell4)->addText("注册(变更)日期", null, $styleParagraph);
        $table->addCell(1800, $styleCell5);
        $table->addRow();
        $table->addCell(1800, $styleCell)->addText("首次核发", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['reg_plate_number'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['reg_color_of_plate_number'], null, $styleParagraph);
        $table->addCell(1800, $styleCell4)->addText($basicInfo['reg_date_of_plate_number'], null, $styleParagraph);
        $table->addCell(1800, $styleCell5);
        $countCar = count($carInfo);
        for ($i = 0; $i < $countCar; $i++) {
            $table->addRow();
            $table->addCell(1800, $styleCell)->addText("牌号变更" . ($i + 1), null, $styleParagraph);
            $table->addCell(1800, $styleCell)->addText($carInfo[$i]['change_plate_number'], null, $styleParagraph);
            $table->addCell(1800, $styleCell)->addText($carInfo[$i]['change_plate_number_color'], null, $styleParagraph);
            $table->addCell(1800, $styleCell4)->addText($carInfo[$i]['change_plate_number_date'], null, $styleParagraph);
            $table->addCell(1800, $styleCell5);
        }
        $table->addRow();
        $table->addCell(1800, $styleCell2)->addText("道路运输证信息", null, $styleParagraph);
        $table->addCell(1800, $styleCell5);
        $table->addRow();
        $table->addCell(1800, $styleCell)->addText("", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("业户名称", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("道路运输证号", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("经营范围", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("发证日期", null, $styleParagraph);
        $table->addCell(1800, $styleCell5);
        $table->addRow();
        $table->addCell(1800, $styleCell)->addText("初次登记", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['reg_owner_name'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['reg_transport_id'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['reg_business_scope'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['reg_license_date'], null, $styleParagraph);
        $table->addCell(1800, $styleCell5);
        $countOwner = count($ownerInfo);
        for ($i = 0; $i < $countOwner; $i++) {
            $table->addRow();
            $table->addCell(1800, $styleCell)->addText("名称变更" . ($i + 1), null, $styleParagraph);
            $table->addCell(1800, $styleCell)->addText($ownerInfo[$i]['change_owner_name'], null, $styleParagraph);
            $table->addCell(1800, $styleCell)->addText($ownerInfo[$i]['change_transport_id'], null, $styleParagraph);
            $table->addCell(1800, $styleCell)->addText($ownerInfo[$i]['change_business_scope'], null, $styleParagraph);
            $table->addCell(1800, $styleCell)->addText($ownerInfo[$i]['change_license_date'], null, $styleParagraph);
            $table->addCell(1800, $styleCell5);
        }
        $table->addRow();
        $table->addCell(1800, $styleCell6)->addText("车辆配置及主要技术参数", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("车辆类型", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['car_type'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("厂牌型号", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['factory_model'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("制造厂商", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['factory_name'], null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        $table->addCell(1800, $styleCell)->addText("出厂日期", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['complete_date'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("国产进口", null, $styleParagraph);
        if (intval($basicInfo['product_area']) == 1) {
            $product_area = "国产";
        } elseif (intval($basicInfo['product_area']) == 2) {
            $product_area = "进口";
        } else {
            $product_area = "-";
        }
        $table->addCell(1800, $styleCell)->addText($product_area, null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("VIN(或车架)号", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['vin_number'], null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        $table->addCell(1800, $styleCell)->addText("底盘型号", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['chassis_number'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("车辆外廓尺寸", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['gabarite'] . " mm", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("货箱内尺寸或容积", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['container_size'], null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        $table->addCell(1800, $styleCell)->addText("总质量", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['all_up_weight'] . " kg", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("整备质量", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['prepare_weight'] . " kg", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("准牵引质量", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['pull_weight'] . " kg", null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        $table->addCell(1800, $styleCell)->addText("核定载质量", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['standard_load_weight'] . " kg", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("核定载客", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['standard_load_manned_number'] . " 人", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("发动机型号", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['engine_model'], null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        $table->addCell(1800, $styleCell)->addText("发动机号码", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['engine_number'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("发动机排量", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['engine_displacement'] . " L", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("发动机净功率", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['engine_power'] . " kw", null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        $table->addCell(1800, $styleCell)->addText("排放标准", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['displacement_line'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("电池类型", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['battery_type'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("驱动电机型号", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['electric_machine_model'], null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        $table->addCell(1800, $styleCell)->addText("电机功率", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['electric_machine_power'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("动力类型", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['power_type'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("车轴数量", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['axle_number'], null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        $table->addCell(1800, $styleCell)->addText("轴距", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['axle_distance'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("轮胎数/规格", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($basicInfo['tyre_number'], null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("行车制动方式", null, $styleParagraph);
        if (intval($basicInfo['braking_class']) == 1) {
            $braking_class = "气";
        } elseif (intval($basicInfo['braking_class']) == 2) {
            $braking_class = "液";
        } elseif (intval($basicInfo['braking_class']) == 3) {
            $braking_class = "气-液";
        } else {
            $braking_class = "-";
        }
        $table->addCell(1800, $styleCell)->addText($braking_class, null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        if (intval($basicInfo['braking_type_forward']) == 1) {
            $braking_type_forward = "盘式";
        } elseif (intval($basicInfo['braking_type_forward']) == 2) {
            $braking_type_forward = "鼓式";
        } else {
            $braking_type_forward = "-";
        }
        if (intval($basicInfo['braking_type_backward']) == 1) {
            $braking_type_backward = "盘式";
        } elseif (intval($basicInfo['braking_type_backward']) == 2) {
            $braking_type_backward = "鼓式";
        } else {
            $braking_type_backward = "-";
        }
        $table->addCell(1800, $styleCell)->addText("制动器形式", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText("前轮：" . $braking_type_forward . "
后轮：" . $braking_type_backward, null, $styleParagraph);
        if (intval($basicInfo['abs']) == 1) {
            $abs = "有";
        } elseif (intval($basicInfo['abs']) == 0) {
            $abs = "无";
        } else {
            $abs = "-";
        }
        $table->addCell(1800, $styleCell)->addText("制动防拖死系统（ABS）", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($abs, null, $styleParagraph);
        if (intval($basicInfo['speed_changer_type']) == 1) {
            $speed_changer_type = "手动";
        } elseif (intval($basicInfo['speed_changer_type']) == 2) {
            $speed_changer_type = "自动";
        } elseif (intval($basicInfo['speed_changer_type']) == 3) {
            $speed_changer_type = "手自一体";
        } else {
            $speed_changer_type = "-";
        }
        $table->addCell(1800, $styleCell)->addText("变速器形式", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($speed_changer_type, null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell7);
        if (intval($basicInfo['buffer']) == 1) {
            $buffer = "有";
        } elseif (intval($basicInfo['buffer']) == 0) {
            $buffer = "无";
        } else {
            $buffer = "-";
        }
        $table->addCell(1800, $styleCell)->addText("缓速器", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($buffer, null, $styleParagraph);
        if (intval($basicInfo['air_condition_system']) == 1) {
            $air_condition_system = "有";
        } elseif (intval($basicInfo['air_condition_system']) == 0) {
            $air_condition_system = "无";
        } else {
            $air_condition_system = "-";
        }
        $table->addCell(1800, $styleCell)->addText("空调系统", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($air_condition_system, null, $styleParagraph);
        if (intval($basicInfo['gps']) == 1) {
            $gps = "有";
        } elseif (intval($basicInfo['gps']) == 0) {
            $gps = "无";
        } else {
            $gps = "-";
        }
        $table->addCell(1800, $styleCell)->addText("卫星定位装置", null, $styleParagraph);
        $table->addCell(1800, $styleCell)->addText($gps, null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell)->addText("备注", null, $styleParagraph);
        $table->addCell(1800, $styleCell8)->addText("", null, $styleParagraph);
        $table->addRow();
        $table->addCell(1800, $styleCell9)->addText("注1：货箱内尺寸或容积：普通栏客车、厢式车、仓篷车、自卸车等填写货箱内尺寸，罐式车写容积。
注2：电池类型、驱动电机型号和电动功率。纯电动汽车填写，其他车辆不用填写。
注3：排放标准：指国Ⅲ、国Ⅳ、国Ⅴ或其他排放阶段，纯电动车不用填写。
注4：动力类型：指汽油、柴油、纯电动、液化天然气（LNG）、压缩天然气（CNG）、液化石油气（LPG）或其他。
注5：请填写其他或选择有关信息，符合的请在选择项上以“√”表示。");
    }
}